package cn.clementine.core.entity;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author clementine
 * 打印硬盘信息
 *
 */
@Slf4j
public class DiskInfo {
    public List<Disk> getDiskInfo()  {
        List<Disk> diskList = new ArrayList<>();
        DecimalFormat df = new DecimalFormat("#0.00");
        File[] disks = File.listRoots();
        try{
            for (File file : disks) {
                // 获取总容量
                long totalSpace = file.getTotalSpace();
                // 获取剩余容量
                long usableSpace = file.getUsableSpace();
                // 获取已经使用的容量
                long freeSpace = totalSpace - usableSpace;
                // 获取使用率
                float useRate = (float)((freeSpace * 1.0 / totalSpace) * 100);
                Disk disk = new Disk(file.getCanonicalPath(), transformation(totalSpace), transformation(freeSpace), transformation(usableSpace), Double.parseDouble(df.format(useRate)) + "%");
                diskList.add(disk);
            }
        }catch (IOException e){
            log.error(e.getMessage());
            return diskList;
        }
        return diskList;
    }
    private String transformation(long size){
        return size / 1024 / 1024 / 1024 + "GB"+"   ";
    }
}
